package gr.atc.epart.googlewrapper.accounts;

import gr.atc.epart.EPartVolatileDataStore;
import gr.atc.epart.R;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.util.Pair;

public class GoogleAccountCredentialStorage
{
	public static final String MODERATOR = "moderator";
	public static final String GMAIL = "gmail";
	public static final String YOUTUBE = "youtube";
	public static final String PICASSAWEB = "picassawebalbums";
	public static final String CALENDAR = "calendar";
	
	// we need to store the following
	// service name -- last auth token -- user name -- password
	// "moderator"    "AE455DD..."      "user"         "pwd" 
	private Map<String,Pair<String,Pair<String,String> > > _credentialsStorage;
	private String FILENAME = "credentials.dat";
	
	private static GoogleAccountCredentialStorage _instance = new GoogleAccountCredentialStorage();

	 
	private GoogleAccountCredentialStorage()
	{
		if (!loadCredentials())
			_credentialsStorage = new HashMap<String,Pair<String, Pair<String,String>>>();	
	}
	
	public static GoogleAccountCredentialStorage getInstance()
	{
		return _instance;
	}
	
	public Pair<String,String> getCredentials(String service)
	{
		if (_credentialsStorage.containsKey(service))
		{
			return _credentialsStorage.get(service).second;
		}
		else
		{
			return null;
		}
	}
	
	public String getLastUsedAuthToken(String service)
	{
		if (_credentialsStorage.containsKey(service))
		{
			return _credentialsStorage.get(service).first;
		}
		else
		{
			return null;
		}
	}
	
	public String getUsername(String service)
	{
		Pair<String,String > credentials  = getCredentials(service);

		if (credentials!= null)
		{
			return credentials.first;
		}
		else
		{
			return null;
		}
	}
		
	public String getPassword(String service)
	{
		Pair<String,String > credentials  = getCredentials(service);

		if (credentials!= null)
		{
			return credentials.second;
		}
		else
		{
			return null;
		}

	}
	
	public void setCredentials(String service,String username,String password)
	{
		Pair<String,String> newCredentials = new Pair<String, String>(username,password);
		Pair<String,Pair<String,String>> newAuthCredentials = new Pair<String,Pair<String,String>> (getLastUsedAuthToken(service),newCredentials);
		
		_credentialsStorage.remove(service);
		_credentialsStorage.put(service,newAuthCredentials);

	}

	public void setUsername(String service,String username)
	{
		String password = getPassword(service);
		setCredentials(service, username, password);
	}

	public void setPassword(String service,String password )
	{
		String username = getUsername(service);
		setCredentials(service, username, password);
	}

	public void saveCredentials()
	{
		try
		{
			FileOutputStream credentialsStorageFile = new FileOutputStream(FILENAME);
        	ObjectOutputStream out = new ObjectOutputStream(credentialsStorageFile);
			out.writeObject(_credentialsStorage);
			out.close();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	private boolean loadCredentials()
	{
		try
		{
		    FileInputStream credentialsStorageFile = new FileInputStream(FILENAME);
		    ObjectInputStream in = new ObjectInputStream(credentialsStorageFile);
		    _credentialsStorage = (Map<String,Pair<String,Pair<String,String>>>) in.readObject();
			in.close();
			return true;
			
		} catch (IOException e)
		{
			
			e.printStackTrace();
			
		} catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		}
		
		
		return false;
	}
	
}
